<template>
  <div class="sideMenu">
    <div class="menuBody" @mouseover="move" @mouseleave="out">
      <div class="menu">
        <div
          :class="['item',{'active':index==hoverAct}]"
          v-for="(item,index) in menu"
          :key="index"
          @mouseover="hoverShow(index)"
        >
          <span>{{item.type}}</span>
          <span class="iconfont icon-jiantou-copy"></span>
        </div>
      </div>
      <div class="subMenu" v-if="isShowSideMenu">
        <div
          class="item"
          v-for="(item,index) in childrenMenu"
          :key="index"
          @click="searchCity(item.city)"
        >
          <nuxt-link to>
            <span>{{index+1}}</span>
            <span>{{item.city}}</span>
            <span>{{item.desc}}</span>
          </nuxt-link>
        </div>
      </div>
    </div>
    <!-- 推荐城市 -->
    <div class="recommend">
      <h4>推荐城市</h4>
      <nuxt-link to>
        <img src="http://157.122.54.189:9093/images/pic_sea.jpeg" alt />
      </nuxt-link>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menu: [],
      hoverAct: null,
      isShowSideMenu: false,
      childrenMenu: []
    };
  },
  created() {
    this.getMenu();
  },
  methods: {
    //获取侧边菜单
    getMenu() {
      this.$axios({
        url: "/posts/cities"
      }).then(res => {
        this.menu = res.data.data;
      });
    },
    //经过菜单操作
    hoverShow(index) {
      this.hoverAct = index;
      this.childrenMenu = this.menu[index].children;
    },
    //移入显示更多菜单
    move() {
      this.isShowSideMenu = true;
    },
    //移除隐藏更多菜单并且移除类
    out() {
      this.isShowSideMenu = false;
      this.hoverAct = null;
    },
    // 搜索
    searchCity(item) {
      this.$emit("sideChange", item);
    }
  }
};
</script>

<style lang="less" scoped>
.sideMenu {
  width: 260px;
  .menuBody {
    position: relative;
    .item {
      height: 40px;
      line-height: 40px;
      padding: 0 20px;

      cursor: pointer;
    }
    .menu {
      width: 100%;
      border: 1px solid #ddd;
      border-right: none;
      position: relative;
      z-index: 2;
      .item {
        border-bottom: 1px solid #ddd;
        border-right: 1px solid #ddd;
        justify-content: space-between;
        display: flex;
        span {
          font-size: 14px;
        }
        &:last-child {
          border-bottom: none;
        }
      }
      .active {
        color: #ffa500;
        border-right-color: #fff;
      }
    }
    .subMenu {
      position: absolute;
      top: 0;
      right: -299px;
      width: 300px;
      border: 1px solid #ddd;
      background: #fff;
      z-index: 1;
      .item {
        font-size: 12px;
        vertical-align: middle;
        a {
          span {
            &:nth-child(1) {
              font-style: italic;
              font-size: 24px;
              color: #ffa500;
              vertical-align: sub;
            }
            &:nth-child(2) {
              color: #ffa500;
              margin: 0 10px;
            }
            &:nth-child(3) {
              color: #999;
              &:hover {
                text-decoration: underline;
              }
            }
          }
        }
      }
    }
  }
  .recommend {
    padding-top: 20px;
    h4 {
      font-weight: 400;
      padding-bottom: 10px;
      border-bottom: 1px solid #ddd;
      margin-bottom: 10px;
    }
    img {
      width: 100%;
    }
  }
}
</style>